import axios from "axios"
import router from "../router/index.js"
import { Message } from "element-ui"

let instance = axios.create({
  baseURL: "/api",
  timeout: 5000
})

// 拦截器
instance.interceptors.request.use(
  config => {
    return config
  },
  e => {
    Message.error("请求失败")
    console.log("请求发送失败" + e)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  res => {
    if (res.data.status === 415) {
      Message.warning("请重新登录")
      window.sessionStorage.removeItem("user")
      setTimeout(() => {
        router.replace("/login")
      }, 2000)
    }
    return res
  },
  () => {
    Message.error("服务器异常")
  }
)

export default instance
